import { isURL } from '@/util/validate'
import { formatPath } from '@/util/util'
// 将参数处理为参数的形式拼接
const objToform = o =>
  Object.keys(o)
    .map(it => `${it}=${o[it]}`)
    .join('&')

const RouterPlugin = function () {
  this.$router = null
  this.$store = null
}
RouterPlugin.install = function (Vue, option = {}) {
  this.$router = option.router
  this.$store = option.store
  this.$vue = new Vue({ i18n: option.i18n })
  const closeTag = value => {
    let tag = value || this.$store.getters.tag
    if (typeof value === 'string') {
      tag = this.$store.getters.tagList.filter(ele => ele.value === value)[0]
    }
    this.$store.commit('DEL_TAG', tag)
  }
  this.$router.$avueRouter = {
    //全局配置
    $website: this.$store.getters.website,
    group: '',
    meta: {},
    safe: this,
    closeTag,
    vaildPath: (list, path) => list.some(el => new RegExp('^' + el + '.*', 'g').test(path)),
    getValue: route => route.query.src || route.path, // 设置路由值
    setTitle: title => {
      document.title = title ? `${title}-${this.$vue.$t('title')}` : this.$vue.$t('title')
    },
    generateTitle: (title, key) => {
      return !key ? title : this.$vue.$te('route.' + key) ? this.$vue.$t('route.' + key) : title
    },
    getPath: params => {
      params.src = isURL(params.src) ? `/myiframe/urlPath?${objToform(params)}` : params.src || '/'
      return params.src
    },

    /**
     * menus: 接受到的动态路由数据 menu的结构外层有父级path 里面有一个childen 记录页面的路由
     * first: 为了区分外界 调用formatRoutes 和 当前文件调用 formatRoutes
     */
    formatRoutes(menus = [], isInit = true) {
      // 如果没有权限菜单就结束
      if (menus.length === 0) return

      const Routes = []

      // 开始处理menu
      for (let i = 0; i < menus.length; i++) {
        // 取到当前要处理的一项
        const { path, name, icon, children } = menus[i] || {}
        const meta = Object.assign(menus[i].meta || {}, { keepAlive: option.keepAlive === true })
        const route = {
          path: formatPath(path),
          name,
          icon,
          meta,
          component: () => {
            // 初始化
            if (isInit) {
              return import(/* webpackChunkName: "base" */ '@/layout/basic/index')
              // 判断是否为多层路由
            } else if (children.length && !isInit) {
              return import(/* webpackChunkName: "base" */ '@/layout/basic/index')
              // 判断是否为最终的页面视图
            } else {
              return import(/* webpackChunkName: "page" */ `@/views${path}.vue`)
            }
          },
          children: children.length
            ? this.formatRoutes(children, false)
            : [
                {
                  icon,
                  name,
                  meta,
                  path: '',
                  component: () => import(/* webpackChunkName: "page" */ `@/views${path}.vue`)
                }
              ]
        }
        if (!isURL(path)) {
          if (children.length) {
            route.redirect = formatPath(children[0].path)
          }
        }

        Routes.push(route)
      }
      if (isInit) {
        this.safe.$router.addRoutes(Routes)
        return
      }
      return Routes
    }
  }
}
export default RouterPlugin
